﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZY.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZY.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:View_Tab_Poistion
    /// </summary>
    public partial class View_Tab_Poistion : IView_Tab_Poistion
    {
        public View_Tab_Poistion()
        { }
        #region  BasicMethod


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZY.Model.View_Tab_Poistion model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into View_Tab_Poistion(");
            strSql.Append("p_name,up_name,p_createtime,p_lastime,p_note,pleve,p_ext1,p_ext2,p_ext3)");
            strSql.Append(" values (");
            strSql.Append("@p_name,@up_name,@p_createtime,@p_lastime,@p_note,@pleve,@p_ext1,@p_ext2,@p_ext3)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@p_name", SqlDbType.VarChar,50),
					new SqlParameter("@up_name", SqlDbType.VarChar,50),
					new SqlParameter("@p_createtime", SqlDbType.DateTime),
					new SqlParameter("@p_lastime", SqlDbType.DateTime),
					new SqlParameter("@p_note", SqlDbType.VarChar,5000),
					new SqlParameter("@pleve", SqlDbType.VarChar,4),
					new SqlParameter("@p_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@p_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@p_ext3", SqlDbType.VarChar,50)};
            parameters[0].Value = model.p_name;
            parameters[1].Value = model.up_name;
            parameters[2].Value = model.p_createtime;
            parameters[3].Value = model.p_lastime;
            parameters[4].Value = model.p_note;
            parameters[5].Value = model.pleve;
            parameters[6].Value = model.p_ext1;
            parameters[7].Value = model.p_ext2;
            parameters[8].Value = model.p_ext3;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZY.Model.View_Tab_Poistion model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update View_Tab_Poistion set ");
            strSql.Append("p_name=@p_name,");
            strSql.Append("up_name=@up_name,");
            strSql.Append("p_createtime=@p_createtime,");
            strSql.Append("p_lastime=@p_lastime,");
            strSql.Append("p_note=@p_note,");
            strSql.Append("pleve=@pleve,");
            strSql.Append("p_ext1=@p_ext1,");
            strSql.Append("p_ext2=@p_ext2,");
            strSql.Append("p_ext3=@p_ext3");
            strSql.Append(" where pid=@pid");
            SqlParameter[] parameters = {
					new SqlParameter("@p_name", SqlDbType.VarChar,50),
					new SqlParameter("@up_name", SqlDbType.VarChar,50),
					new SqlParameter("@p_createtime", SqlDbType.DateTime),
					new SqlParameter("@p_lastime", SqlDbType.DateTime),
					new SqlParameter("@p_note", SqlDbType.VarChar,5000),
					new SqlParameter("@pleve", SqlDbType.VarChar,4),
					new SqlParameter("@p_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@p_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@p_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@pid", SqlDbType.Int,4)};
            parameters[0].Value = model.p_name;
            parameters[1].Value = model.up_name;
            parameters[2].Value = model.p_createtime;
            parameters[3].Value = model.p_lastime;
            parameters[4].Value = model.p_note;
            parameters[5].Value = model.pleve;
            parameters[6].Value = model.p_ext1;
            parameters[7].Value = model.p_ext2;
            parameters[8].Value = model.p_ext3;
            parameters[9].Value = model.pid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int pid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from View_Tab_Poistion ");
            strSql.Append(" where pid=@pid");
            SqlParameter[] parameters = {
					new SqlParameter("@pid", SqlDbType.Int,4)
			};
            parameters[0].Value = pid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string pidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from View_Tab_Poistion ");
            strSql.Append(" where pid in (" + pidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZY.Model.View_Tab_Poistion GetModel(int pid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 pid,p_name,up_name,p_createtime,p_lastime,p_note,pleve,p_ext1,p_ext2,p_ext3 from View_Tab_Poistion ");
            strSql.Append(" where pid=@pid");
            SqlParameter[] parameters = {
					new SqlParameter("@pid", SqlDbType.Int,4)
			};
            parameters[0].Value = pid;

            ZY.Model.View_Tab_Poistion model = new ZY.Model.View_Tab_Poistion();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZY.Model.View_Tab_Poistion DataRowToModel(DataRow row)
        {
            ZY.Model.View_Tab_Poistion model = new ZY.Model.View_Tab_Poistion();
            if (row != null)
            {
                if (row["pid"] != null && row["pid"].ToString() != "")
                {
                    model.pid = int.Parse(row["pid"].ToString());
                }
                if (row["p_name"] != null)
                {
                    model.p_name = row["p_name"].ToString();
                }
                if (row["up_name"] != null)
                {
                    model.up_name = row["up_name"].ToString();
                }
                if (row["p_createtime"] != null && row["p_createtime"].ToString() != "")
                {
                    model.p_createtime = DateTime.Parse(row["p_createtime"].ToString());
                }
                if (row["p_lastime"] != null && row["p_lastime"].ToString() != "")
                {
                    model.p_lastime = DateTime.Parse(row["p_lastime"].ToString());
                }
                if (row["p_note"] != null)
                {
                    model.p_note = row["p_note"].ToString();
                }
                if (row["pleve"] != null)
                {
                    model.pleve = row["pleve"].ToString();
                }
                if (row["p_ext1"] != null)
                {
                    model.p_ext1 = row["p_ext1"].ToString();
                }
                if (row["p_ext2"] != null)
                {
                    model.p_ext2 = row["p_ext2"].ToString();
                }
                if (row["p_ext3"] != null)
                {
                    model.p_ext3 = row["p_ext3"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select pid,p_name,up_name,p_createtime,p_lastime,p_note,pleve,p_ext1,p_ext2,p_ext3 ");
            strSql.Append(" FROM View_Tab_Poistion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" pid,p_name,up_name,p_createtime,p_lastime,p_note,pleve,p_ext1,p_ext2,p_ext3 ");
            strSql.Append(" FROM View_Tab_Poistion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM View_Tab_Poistion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.pid desc");
            }
            strSql.Append(")AS Row, T.*  from View_Tab_Poistion T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "View_Tab_Poistion";
            parameters[1].Value = "pid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

